from django.db import models
from django.utils.translation import gettext as _
from django.db.models.signals import pre_delete
from django.dispatch.dispatcher import receiver


class CarouselContent(models.Model):
    """首页轮播图"""
    title_zh = models.CharField(max_length=128)
    title_en = models.CharField(max_length=128)
    description_zh = models.TextField(max_length=512)
    description_en = models.TextField(max_length=512)
    picture = models.ImageField(upload_to='_img/%Y/%m/%d/')
    published = models.BooleanField(default=False)
    pub_date = models.DateField(auto_now=True)

    class Meta:
        verbose_name = _('Carousel Content at Home Page')
        verbose_name_plural = _('Carousel Contents at Home Page')

    def __str__(self):
        return self.title_zh

    def get_content(self, lang):
        if lang.startswith('zh'):
            return {'title': self.title_zh,
                    'description': self.description_zh,
                    'picture': self.picture}
        else:
            return {'title': self.title_en,
                    'description': self.description_en,
                    'picture': self.picture}


@receiver(pre_delete, sender=CarouselContent)
def carousel_delete(sender, instance, **kwargs):
    instance.picture.delete(False)


# class DatabaseBackup(models.Model):
#     """数据库备份文件"""
#     db_type = models.CharField(max_length=32, choices=(('m', 'mongodb'),
#                                                        ('p', 'postgresql')))
#     back_date = models.DateField(auto_now=True)
#     file = models.FileField(upload_to='_fs/%Y/%m/%d/')
#
#     class Meta:
#         verbose_name = _('Database Backup File')
#         verbose_name_plural = _('Database Backup Files')
#
#     def delete(self, using=None, keep_parents=False):
#         super().delete(using, keep_parents)
#         self.file.delete()
